<?php

function sendNotify($url)
{

    $ch = curl_init();
    $timeout = 5;
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $file_contents = curl_exec($ch);
    curl_close($ch);
    return true;
}

/**
 * 发起网络请求
 * @param $url
 * @param string $post
 * @param string $cookie
 * @param int $returnCookie
 * @return bool|string
 */
function curl_request ( $url , $post = '' , $cookie = '' ,  $returnCookie = 0 ) {
    $curl  =  curl_init ( ) ;
    curl_setopt ( $curl , CURLOPT_URL ,  $url ) ;
    curl_setopt ( $curl , CURLOPT_USERAGENT ,  'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)' ) ;
    curl_setopt ( $curl , CURLOPT_FOLLOWLOCATION ,  1 ) ;
    curl_setopt ( $curl , CURLOPT_AUTOREFERER ,  1 ) ;
    curl_setopt ( $curl , CURLOPT_REFERER ,  "http://XXX" ) ;
    if ( $post )  {
        curl_setopt ( $curl , CURLOPT_POST ,  1 ) ;
        curl_setopt ( $curl , CURLOPT_POSTFIELDS ,  http_build_query ( $post ) ) ;
    }
    if ( $cookie )  {
        curl_setopt ( $curl , CURLOPT_COOKIE ,  $cookie ) ;
    }
    curl_setopt ( $curl , CURLOPT_HEADER ,  $returnCookie ) ;
    curl_setopt ( $curl , CURLOPT_TIMEOUT ,  10 ) ;
    curl_setopt ( $curl , CURLOPT_RETURNTRANSFER ,  1 ) ;
    $data  =  curl_exec ( $curl ) ;
    if  ( curl_errno ( $curl ) )  {
        return  curl_error ( $curl ) ;
    }
    curl_close ( $curl ) ;
    if ( $returnCookie ) {
        list ( $header ,  $body )  =  explode ( "\r\n\r\n" ,  $data ,  2 ) ;
        preg_match_all ( "/Set\-Cookie:([^;]*);/" ,  $header ,  $matches ) ;
        $info [ 'cookie' ]   =  substr ( $matches [ 1 ] [ 0 ] ,  1 ) ;
        $info [ 'content' ]  =  $body ;
        return  $info ;
    } else {
        return  $data ;
    }
}

/**
 * 打乱数组顺序
 * @param $list
 * @return array
 */
function shuffle_assoc($list) {
    if (!is_array($list)) return $list;

    $keys = array_keys($list);
    shuffle($keys);
    $random = array();
    foreach ($keys as $key)
        $random[$key] = shuffle_assoc($list[$key]);
    return $random;
}

/**
 * 获取企业微信相关系统配置
 * @param $type
 * @return string
 */
function getWxworkKey($type)
{
    $wxwork = 'wxwork.';
    $wxworkKey = \App\Models\SystemConfig::findValue($wxwork.$type);
    if ($wxworkKey == null) {
        $wxworkKey = \App\Models\SystemConfig::findValue($wxwork.'common');
    }
    return $wxworkKey;
}

/**
 * 获取流量单实际派单时间
 * @param $start
 * @param $end
 * @return float|int|mixed|string
 */
function getRealHours($start, $end)
{
    $startDate = date('d-H-i', $start);
    $startDateArr = explode('-', $startDate);
    $endDate = date('d-H-i', $end);
    $endDateArr = explode('-', $endDate);
    $subHour = 0;
    if ($startDateArr[1] < 6) {
        $subHour = 6 - $startDateArr[1];
    }
    $addHour = $endDateArr[2] > $startDateArr[2] ? 0 : -1;
    $day = $endDateArr[0] - $startDateArr[0];
    $totalHours = $endDateArr[1] - $startDateArr[1] + 24 * $day - 6 * $day - $subHour + $addHour;
    return $totalHours;
}

/**
 * 企业微信机器人
 * @param $content
 * @param null $wxKey
 * @param false $skipDev
 * @return bool|string
 */
function logWxwork($content, $wxKey = null, $skipDev = false)
{
    if ($wxKey == null) {
        $key = getWxworkKey('common');
        $url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key='.$key;
    }else{
        $url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key='.$wxKey;
    }
    $headers = [];
    array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
    $env = "Laravel\n环境";
    $data = '{
        "msgtype": "text",
        "text": {
            "content": "'.$content.$env.'",
            "mentioned_list":["@all"]
        }
    }';

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    $output = curl_exec($curl);
    if (curl_errno($curl)) {
        echo "Errno". curl_error($curl); // 抓捕异常
    }
    curl_close($curl);
    return $output;
}

